from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from tools import TableStu
from tools.MyWidget import IndicatorLine
from tools.BaseInfo import Course
import json
import os
import sys
import final

class LeftWidget(QWidget):
    def __init__(self) -> None:
        super().__init__()
        self.btn0 = QPushButton("指标查询",self)
        self.btn1 = QPushButton("指标录入",self)
        self.btn2 = QPushButton("结果查询",self)
        self.setFixedWidth(135)
        #self.setFixedSize(134,370)
        self.init_ui()
        self.set_widget_style()
    def stulist(self):
        for i in range(len(self.right)):
            self.right[i].close()
        self.right[1].show()
        self.right_layout.addWidget(self.right[1])
        self.btn0.setStyleSheet("color:rgb(170,170,170);border:none;")
        self.btn1.setStyleSheet("color:rgb(73,166,234);border:none;")
        self.btn2.setStyleSheet("color:rgb(170,170,170);border:none;")
    def result(self):
        for i in range(len(self.right)):
            self.right[i].close()
        self.right[2].show()
        self.right_layout.addWidget(self.right[2])
        self.btn0.setStyleSheet("color:rgb(170,170,170);border:none;")
        self.btn1.setStyleSheet("color:rgb(170,170,170);border:none;")
        self.btn2.setStyleSheet("color:rgb(73,166,234);border:none;")
    def home(self):
        for i in range(len(self.right)):
            self.right[i].close()
        self.right[0].show()
        self.right_layout.addWidget(self.right[0])
        self.btn0.setStyleSheet("color:rgb(73,166,234);border:none;")
        self.btn1.setStyleSheet("color:rgb(170,170,170);border:none;")
        self.btn2.setStyleSheet("color:rgb(170,170,170);border:none;")
    def get_widget_pointer(self,widget,layout):
        self.right = widget
        self.right_layout = layout
    def init_ui(self):
        pal = QPalette(self.palette())
        pal.setColor(QPalette.Background,QColor(255,255,255))
        self.setAutoFillBackground(True)
        self.setPalette(pal)
        self.btn0.move(15,40)
        self.btn1.move(15,130)
        self.btn2.move(15,230)
        self.btn0.clicked.connect(self.home)
        self.btn1.clicked.connect(self.stulist)
        self.btn2.clicked.connect(self.result)
    def set_widget_style(self):
        font = QFont("PingFang SC Regular")
        font.setPixelSize(20)
        self.btn0.setFont(font)
        self.btn1.setFont(font)
        self.btn2.setFont(font)
        self.btn0.setStyleSheet("color:rgb(73,166,234);border:none;")
        self.btn1.setStyleSheet("color:rgb(170,170,170);border:none;")
        self.btn2.setStyleSheet("color:rgb(170,170,170);border:none;")
class TopWidget(QWidget):
    def __init__(self) -> None:
        super().__init__()
        pal = QPalette(self.palette())
        pal.setColor(QPalette.Background,QColor(255,255,255))
        self.setAutoFillBackground(True)
        self.setPalette(pal)
        self.layout = QHBoxLayout()
        self.layout.setContentsMargins(0,0,0,0)
        self.setLayout(self.layout)
        #设置logo
        self.left_pic_widget = QWidget()
        self.logo = QLabel(self.left_pic_widget)
        self.pix = QPixmap("static/cqu.jpg")
        self.pix = self.pix.scaled(39, 39, Qt.KeepAspectRatio)
        self.logo.setPixmap(QPixmap(self.pix))
        #设置标题
        self.tittle = QLabel(self.left_pic_widget)
        self.tittle.setText("毕业指标管理")
        font = QFont('PingFang SC Regular',20)
        font.setPixelSize(20)
        self.tittle.setFont(font)
        self.layout.addWidget(self.left_pic_widget)
        self.tittle.move(55,0)
        self.side = QLabel(self)
        self.side.setFixedSize(60,35)
        self.pic = QPixmap("static/people.jpg")
        self.pic = self.pic.scaled(42, 33, Qt.KeepAspectRatio)
        self.side.setPixmap(QPixmap(self.pic))
        self.layout.addWidget(self.side)
        self.setFixedHeight(57)

class Test(QWidget):
    def __init__(self):
        super().__init__()
        self.setGeometry(350,250,754,460)
        self.layout = QVBoxLayout()
        self.setLayout(self.layout)
        
        self.main_wid = QWidget()
        self.main_lay = QHBoxLayout()
        self.main_wid.setLayout(self.main_lay)
        self.left = LeftWidget()
        self.main_lay.addWidget(self.left)
        self.middle = [RightWidget(),StuListWidget(),final.RightWidget()]
        self.main_lay.addWidget(self.middle[0])
        self.top = TopWidget()
        self.layout.addWidget(self.top)
        self.layout.addWidget(self.main_wid)
        self.left.get_widget_pointer(self.middle,self.main_lay)
class RightWidget(QWidget):
    def __init__(self) -> None:
        super().__init__()
        self.setMinimumSize(600,400)
        self.line_list = []
        self.line_list.append(IndicatorLine(self))
        self.line_list.append(IndicatorLine(self))
        self.line_list.append(IndicatorLine(self))
        for i in range(len(self.line_list)):
            self.line_list[i].setMinimumSize(165,65)
            self.line_list[i].move(5,130+i*80)
            self.line_list[i].get_pointer(self)
        self.class_name_line = QLineEdit(self)
        self.class_code_line = QLineEdit(self)
        self.pic_lab = QLabel(self)
        self.lab1 = QLabel("指标查询",self)
        self.lab2 = QLabel("课程名称",self)
        self.lab3 = QLabel("课程编号",self)
        self.lab4 = QLabel("课程目标",self)
        self.lab5 = QLabel("毕业要求",self)
        self.lab6 = QLabel("毕业要求指标点",self)
        self.btn1 = QPushButton("导出已记录的所有课程的信息",self)
        self.btn2 = QPushButton("搜索",self)
        self.btn2.clicked.connect(self.search)
        self.btn3 = QPushButton("删除",self)
        self.btn3.clicked.connect(self.delete_class)
        self.pic = QPixmap("static/search.jpg")
        self.pic = self.pic.scaled(50,50)
        self.pic_lab.setPixmap(self.pic)
        self.init_ui()
        self.set_widget_style()
    def init_ui(self):
        pal = QPalette(self.palette())
        pal.setColor(QPalette.Background,QColor(242,242,242))
        self.setAutoFillBackground(True)
        self.setPalette(pal)
        self.pic_lab.move(5,5)
        self.lab1.move(65,20)
        self.btn1.move(350,20)
        self.lab2.move(5,65)
        self.class_name_line.move(80,65)
        self.lab3.move(215,65)
        self.class_code_line.move(290,65)
        self.btn2.move(430,60)
        self.btn3.move(500,60)
        self.lab4.move(50,100)
        self.lab5.move(240,100)
        self.lab6.move(425,100)
    def set_widget_style(self):
        font = QFont("PingFang SC Regular")
        font.setPixelSize(16)
        self.lab2.setFont(font)
        self.lab3.setFont(font)
        self.lab4.setFont(font)
        self.lab5.setFont(font)
        self.lab6.setFont(font)
        font2 = QFont("PingFang SC Regular")
        font2.setPixelSize(18)
        self.lab1.setFont(font2)
    def search(self):
        classname = self.class_name_line.text()
        classcode = self.class_code_line.text()
        if not os.path.exists(os.path.join("classes",classname+"_"+classcode)):
            QMessageBox.information(self,"未找到课程","未找到课程!",QMessageBox.Yes)
        else:
            with open(os.path.join("classes",classname+"_"+classcode,"class.json")) as f:
                result = f.read()
                result = json.loads(result)["Indicator"]
                for i in range(len(result)):
                    self.line_list[i].setText(result[i])
    def delete_class(self):
        classname = self.class_name_line.text()
        classcode = self.class_code_line.text()
        if not os.path.exists(os.path.join("classes",classname+"_"+classcode)):
            QMessageBox.information(self,"未找到课程","未找到课程!",QMessageBox.Yes)
        else:
            course = Course()
            course.delete_course(classname,classcode)
            os.remove(os.path.join("classes",classname+"_"+classcode,"class.json"))
            os.rmdir(os.path.join("classes",classname+"_"+classcode))
            QMessageBox.information(self,"删除成功","删除成功!",QMessageBox.Yes)
            for i in range(len(self.line_list)):
                self.line_list[i].setText(["","",""])
class TemplatesStu(QWidget):
    def __init__(self,parent=None):
        super().__init__(parent=parent)
        pal = QPalette(self.palette())
        pal.setColor(QPalette.Background,QColor(255,255,255))
        self.setAutoFillBackground(True)
        self.setPalette(pal)
        self.resize(175,305)
        self.pic = QPixmap("static/excel.jpg")
        self.pic = self.pic.scaled(30, 34, Qt.KeepAspectRatio)
        self.lab1 = QLabel(self)
        self.lab1.move(5,5)
        self.lab1.setPixmap(self.pic)
        self.lab2 = QLabel(self)
        self.lab2.setText("学生名单")
        tmp = QFont("PingFang SC Regular",18)
        tmp.setPixelSize(18)
        self.lab2.setFont(tmp)
        self.lab2.move(45,20)
        self.box1 = QCheckBox("辅修",self)
        self.box2 = QCheckBox("重修",self)
        self.box1.move(20,210)
        self.box2.move(90,210)
        self.btn1 = QPushButton("导出模版",self)
        self.btn2 = QPushButton("导入名单",self)
        self.btn1.move(10,245)
        self.btn1.setFixedWidth(75)
        self.btn2.move(90,245)
        self.btn2.setFixedWidth(75)
class TemplatesScore(QWidget):
    def __init__(self,parent=None):
        super().__init__(parent=parent)
        pal = QPalette(self.palette())
        pal.setColor(QPalette.Background,QColor(255,255,255))
        self.setAutoFillBackground(True)
        self.setPalette(pal)
        self.resize(175,305)
        self.pic = QPixmap("static/upload.jpg")
        self.pic = self.pic.scaled(39, 33, Qt.KeepAspectRatio)
        self.lab1 = QLabel(self)
        self.lab1.move(5,5)
        self.lab1.setPixmap(self.pic)
        self.lab2 = QLabel(self)
        self.lab2.setText("成绩录入")
        tmp = QFont("PingFang SC Regular",18)
        tmp.setPixelSize(18)
        self.lab2.setFont(tmp)
        self.lab2.move(45,20)
        self.lab3 = QLabel(self)
        self.lab3.setText("成绩缺少时处理方式")
        f3 = QFont("PingFang SC Regular",16)
        f3.setPixelSize(16)
        self.lab3.setFont(f3)
        self.lab3.move(10,60)
        self.lab4 = QLabel(self)
        self.lab4.setText("包含以下特殊情况")
        f4 = QFont("PingFang SC Regular",16)
        f4.setPixelSize(16)
        self.lab4.setFont(f4)
        self.box1 = QRadioButton("去除学生（不统计）",self)
        self.box2 = QRadioButton("缺少部分置零（统计）",self)
        
        self.box1.move(10,90)
        self.box2.move(10,115)
        self.lab4.move(10,140)
        self.btn1 = QPushButton("导出模版",self)
        self.btn2 = QPushButton("导入成绩",self)
        self.btn1.move(5,245)
        self.btn1.setFixedWidth(75)
        self.btn2.move(90,245)
        self.btn2.setFixedWidth(75) 
class TemplatesResult(QWidget):
    def __init__(self,parent=None):
        super().__init__(parent=parent)
        pal = QPalette(self.palette())
        pal.setColor(QPalette.Background,QColor(255,255,255))
        self.setAutoFillBackground(True)
        self.setPalette(pal)
        self.resize(175,305)
        self.pic = QPixmap("static/statistics.jpg")
        self.pic = self.pic.scaled(37, 27, Qt.KeepAspectRatio)
        self.lab1 = QLabel(self)
        self.lab1.move(5,5)
        self.lab1.setPixmap(self.pic)
        self.lab2 = QLabel(self)
        self.lab2.setText("结果生成")
        tmp = QFont("PingFang SC Regular",18)
        tmp.setPixelSize(18)
        self.lab2.setFont(tmp)
        self.lab2.move(45,20)
        self.box1 = QCheckBox("留学生（包含打勾）",self)
        self.box2 = QCheckBox("学院筛选",self)
        self.box1.move(10,80)
        self.box2.move(10,110)
        self.btn1 = QPushButton("导出存档报告",self)
        self.btn1.setFixedWidth(70)
        self.btn2 = QPushButton("导出提交报告",self)
        self.btn2.setFixedWidth(70)
        self.btn3 = QPushButton("预览",self)
        self.btn4 = QPushButton("导出表",self)
        self.btn3.move(10,150)
        self.btn4.move(90,150)
        self.line1 = QLineEdit(self)
        self.line1.setFixedWidth(60)
        self.line1.move(90,110)
        self.btn1.move(10,200)
        self.btn1.setFixedWidth(150)
        self.btn2.move(10,245)
        self.btn2.setFixedWidth(150)
#指标录入
class StuListWidget(QWidget):
    def __init__(self) -> None:
        super().__init__()
        self.resize(600,400)
        self.line_list = []
        self.line_list.append(IndicatorLine(self))
        self.line_list.append(IndicatorLine(self))
        self.line_list.append(IndicatorLine(self))
        for i in range(len(self.line_list)):
            self.line_list[i].setSize(165,65)
            self.line_list[i].move(5,130+i*80)
            self.line_list[i].get_pointer(self)
        self.class_name_line = QLineEdit(self)
        self.class_code_line = QLineEdit(self)
        self.pic_lab = QLabel(self)
        self.lab1 = QLabel("指标录入",self)
        self.lab2 = QLabel("课程名称",self)
        self.lab3 = QLabel("课程编号",self)
        self.lab4 = QLabel("课程目标",self)
        self.lab5 = QLabel("毕业要求",self)
        self.lab6 = QLabel("毕业要求指标点",self)
        self.btn1 = QPushButton("录入/重新录入",self)
        self.btn2 = QPushButton("添加目标",self)
        self.btn3 = QPushButton("删除目标",self)
        self.pic = QPixmap("static/zhuzi.jpg")
        self.pic = self.pic.scaled(35,45)
        self.pic_lab.setPixmap(self.pic)
        self.init_ui()
        self.set_widget_style()
    def init_ui(self):
        pal = QPalette(self.palette())
        pal.setColor(QPalette.Background,QColor(242,242,242))
        self.setAutoFillBackground(True)
        self.setPalette(pal)
        self.btn1.clicked.connect(self.submit)
        self.btn2.clicked.connect(self.add_goal)
        self.btn3.clicked.connect(self.del_goal)
        self.pic_lab.move(5,5)
        self.lab1.move(60,20)
        self.btn1.move(440,60)
        self.lab2.move(5,65)
        self.class_name_line.move(80,65)
        self.lab3.move(215,65)
        self.class_code_line.move(290,65)
        self.btn2.move(370,20)
        self.btn3.move(470,20)
        self.lab4.move(50,100)
        self.lab5.move(240,100)
        self.lab6.move(425,100)
    def set_widget_style(self):
        font = QFont("PingFang SC Regular")
        font.setPixelSize(16)
        self.lab2.setFont(font)
        self.lab3.setFont(font)
        self.lab4.setFont(font)
        self.lab5.setFont(font)
        self.lab6.setFont(font)
        font2 = QFont("PingFang SC Regular")
        font2.setPixelSize(18)
        self.lab1.setFont(font2)
    def submit(self):
        result = {"Indicator":[]}
        for i in range(len(self.line_list)):
            text = self.line_list[i].getText()
            result["Indicator"].append(text)
        classname = self.class_name_line.text()
        classcode = self.class_code_line.text()
        if not os.path.exists(os.path.join("classes",classname+"_"+classcode)):
            os.mkdir(os.path.join("classes",classname+"_"+classcode))
        with open(os.path.join("classes",classname+"_"+classcode,"class.json"),"w") as f:
            f.write(json.dumps(result))
        QMessageBox.information(self,"录入成功","录入成功!",QMessageBox.Yes)
    def add_goal(self):
        self.line_list.append(IndicatorLine(self))
        self.line_list[-1].move(5,130+(len(self.line_list)-1)*80)
        self.line_list[-1].setSize(165,65)
        self.line_list[-1].get_pointer(self)
        self.line_list[-1].show()
    def del_goal(self):
        if len(self.line_list)>0:
            self.line_list[-1].close()
            del self.line_list[-1]
#成绩录入
class ResultWidget(QWidget):
    def __init__(self) -> None:
        super().__init__()
        pal = QPalette(self.palette())
        pal.setColor(QPalette.Background,QColor(242,242,242))
        self.setAutoFillBackground(True)
        self.setPalette(pal)
        self.resize(590,390)
        self.table = TableStu.TableImport(self)
        self.table.move(0,60)
        self.pic = QPixmap("static/upload.jpg")
        self.pic = self.pic.scaled(45, 40, Qt.KeepAspectRatio)
        self.lab1 = QLabel(self)
        self.lab1.move(5,5)
        self.lab1.setPixmap(self.pic)
        self.lab2 = QLabel(self)
        self.lab2.setText("成绩录入")
        f = QFont("PingFang SC",18)
        self.lab2.setFont(f)
        self.lab2.move(50,15)
        self.btn1 = QPushButton("平时成绩",self)
        self.btn2 = QPushButton("期末成绩",self)
        self.btn1.move(360,10)
        self.btn2.move(460,10)
if __name__=="__main__":
    app = QApplication(sys.argv)
    window = Test()
    window.show()
    sys.exit(app.exec_())